package edu.filefind.thread;

import java.util.ArrayList;
import java.util.List;

public class MonitorThread extends Thread{
	
	List<FileFinder> runningThread ;
	
	
	public MonitorThread(List<FileFinder> runningThread){
		this.runningThread = runningThread;
	}
	
	
	public void run(){
		
		boolean stop = false;
		
		while(!stop){
			
			for(int i=0;i<runningThread.size();i++){
				
				FileFinder temp = runningThread.get(i);
				if(temp.isTerminated()){
					runningThread.remove(i);
					System.out.println("the "+i+"th thread has stopped!");
				}
			}
			
			if(runningThread.size() == 0){
				stop = true;
			}
			
			
			try{
				Thread.sleep(800);
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}
		
		System.out.println("*****************all sub thread has exit! Now print all found result:************************************************************");
		FileFinder.printAllFoundFiles();
		
	}
	

}
